package com.shizhanzhe.zxbapp.task.newshare;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.Util;


import java.util.List;

/**
 * Created by zz9527 on 2018/10/19.
 */
public class ShareAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private Context mContext;
    private List<ShareList.DataBean> list;
    private String price="";
    private String vipprice="";
    public ShareAdapter(List<ShareList.DataBean> list, Context context,String price,String vipprice) {
        this.list = list;
        this.mContext = context;
        this.price=price;
        this.vipprice=vipprice;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        ViewHolder2 holder2;
        ShareList.DataBean bean = list.get(i);
        int type = getItemViewType(i);
        if (convertView == null) {
            if (type == 0) {
                convertView = inflater.inflate(R.layout.adapter_lv_sharenew, null);
                holder = new ViewHolder();
                holder.img = convertView.findViewById(R.id.share_img);
                holder.vip = convertView.findViewById(R.id.vip);
                holder.title = convertView.findViewById(R.id.tv_share_title);
                holder.type = convertView.findViewById(R.id.type);
                holder.red = convertView.findViewById(R.id.red);
                holder.one = convertView.findViewById(R.id.tv);
                holder.two = convertView.findViewById(R.id.tv2);
                Glide.with(mContext).load(Path.img(bean.getIcon())).apply(new RequestOptions().error(R.mipmap.logo)).into(holder.img);
                holder.title.setText(bean.getAname());
                holder.type.setText("".equals(bean.getClassX())||null==bean.getClassX()?"推荐":bean.getClassX());
                holder.red.setText(Html.fromHtml("<font color=\"#ffffff\">已发放</font><font color=\"#FF0000\">"+ Util.Sub(bean.getTotal())+"</font><font color=\"#ffffff\">元</font>"));
                if ("".equals(price)){
                    holder.one.setText(bean.getPrice() + "");
                    holder.two.setText(bean.getVipprice() + "");
                }else {
                    holder.one.setText(price);
                    holder.two.setText(vipprice);
                }
                if (bean.getIsvip()==1){
                    holder.vip.setVisibility(View.VISIBLE);
                }else {
                    holder.vip.setVisibility(View.GONE);
                }
                convertView.setTag(holder);
            } else if (type == 1) {
                convertView = inflater.inflate(R.layout.adapter_lv_share2, null);
                holder2 = new ViewHolder2();
                holder2.img = convertView.findViewById(R.id.iv);
                holder2.vip = convertView.findViewById(R.id.vip);
                holder2.title = convertView.findViewById(R.id.tv_share_title);
                holder2.type = convertView.findViewById(R.id.type);
                holder2.red = convertView.findViewById(R.id.red);
                holder2.one = convertView.findViewById(R.id.tv);
                holder2.two = convertView.findViewById(R.id.tv2);
                if ("".equals(bean.getIcon())){
                    holder2.img.setVisibility(View.GONE);
                }else {
                    Glide.with(mContext).load(Path.img(bean.getIcon())).apply(new RequestOptions().error(R.mipmap.logo)).into(holder2.img);
                }
                holder2.title.setText(bean.getAname());
                holder2.type.setText("".equals(bean.getClassX())||null==bean.getClassX()?"推荐":bean.getClassX());
                holder2.red.setText(Html.fromHtml("已发放<font color=\"#FF0000\">"+Util.Sub(bean.getTotal())+"</font>元"));
                if ("".equals(price)){
                    holder2.one.setText(bean.getPrice() + "");
                    holder2.two.setText(bean.getVipprice() + "");
                }else {
                    holder2.one.setText(price);
                    holder2.two.setText(vipprice);
                }
                if (bean.getIsvip()==1){
                    holder2.vip.setVisibility(View.VISIBLE);
                }else {
                    holder2.vip.setVisibility(View.GONE);
                }
                convertView.setTag(holder2);
            }

        } else {
            if (type == 0) {
                holder = (ViewHolder) convertView.getTag();
                Glide.with(mContext).load(Path.img(bean.getIcon())).apply(new RequestOptions().error(R.mipmap.logo)).into(holder.img);
                holder.title.setText(bean.getAname());
                holder.type.setText("".equals(bean.getClassX())||null==bean.getClassX()?"推荐":bean.getClassX());
                holder.red.setText(Html.fromHtml("<font color=\"#ffffff\">已发放</font><font color=\"#FF0000\">"+Util.Sub(bean.getTotal())+"</font><font color=\"#ffffff\">元</font>"));
                if ("".equals(price)){
                    holder.one.setText(bean.getPrice() + "");
                    holder.two.setText(bean.getVipprice() + "");
                }else {
                    holder.one.setText(price);
                    holder.two.setText(vipprice);
                }
                if (bean.getIsvip()==1){
                    holder.vip.setVisibility(View.VISIBLE);
                }else {
                    holder.vip.setVisibility(View.GONE);
                }
            } else if (type == 1) {
                holder2 = (ViewHolder2) convertView.getTag();
                Glide.with(mContext).load(Path.img(bean.getIcon())).apply(new RequestOptions().error(R.mipmap.logo)).into(holder2.img);
                holder2.title.setText(bean.getAname());
                holder2.type.setText("".equals(bean.getClassX())||null==bean.getClassX()?"推荐":bean.getClassX());
                holder2.red.setText(Html.fromHtml("已发放<font color=\"#FF0000\">"+Util.Sub(bean.getTotal())+"</font>元"));
                if ("".equals(price)){
                    holder2.one.setText(bean.getPrice() + "");
                    holder2.two.setText(bean.getVipprice() + "");
                }else {
                    holder2.one.setText(price);
                    holder2.two.setText(vipprice);
                }
                if (bean.getIsvip()==1){
                    holder2.vip.setVisibility(View.VISIBLE);
                }else {
                    holder2.vip.setVisibility(View.GONE);
                }
                convertView.setTag(holder2);
            }
        }
        return convertView;
    }

    private static class ViewHolder {
        ImageView img;
        ImageView vip;
        TextView title;
        TextView type;
        TextView red;
        TextView one;
        TextView two;
    }

    private static class ViewHolder2 {
        ImageView img;
        ImageView vip;
        TextView title;
        TextView type;
        TextView red;
        TextView one;
        TextView two;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (list.size()>0){
            int isbig = list.get(position).getIsbig();
            if (isbig==0){
                return 0;
            }else {
                return 1;
            }
        }else {
            return 0;
        }
    }
}
